﻿using System;

namespace JYH.Framework
{
    /// <summary>
    /// 类型转(String扩展)
    /// </summary>
    public static class MyConvert
    {
        /// <summary>
        /// 获取Double值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static double ToDouble(this string s)
        {
            double _d = 0;
            Double.TryParse(s, out _d);
            return _d;
        }

        /// <summary>
        /// 获取Int32值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt(this string s)
        {
            int _d = 0;
            Int32.TryParse(s, out _d);
            return _d;
        }

        /// <summary>
        /// 获取long值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static long ToLong(this string s)
        {
            long _d = 0;
            long.TryParse(s, out _d);
            return _d;
        }

        /// <summary>
        /// 获取Decimal值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Decimal ToDecimal(this string s)
        {
            Decimal _d = 0;
            decimal.TryParse(s, out _d);
            return _d;
        }

        /// <summary>
        /// 获取DateTime值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static DateTime? ToDate(this string s)
        {
            if (s == null) return null;
            var dt = DateTime.Now;
            if (DateTime.TryParse(s, out dt))
                return dt;
            else
                return null;
        }

        /// <summary>
        /// 获取Guid值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Guid? ToGuid(this string s)
        {
            if (string.IsNullOrEmpty(s)) return null;
            Guid _d = Guid.Empty;
            if (Guid.TryParse(s, out _d))
                return _d;
            return null;
        }

        /// <summary>
        /// IsNullOrEmpty判断
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }
    }
}
